export class VuexStore extends DataStore {
    vuex = null
    name = 'VuexStore'
    moduleName = null
  
    constructor(vuex, moduleName = '__vuex_store__') {
      super()
      this.vuex = vuex
      this.moduleName = moduleName
      // 数据同步时只会同步state导致模块未同步，这里用 _mutations 来判断重复注册
      if (vuex._mutations[moduleName + '/__updateStore__']) {
        return
      }
      vuex.registerModule(moduleName, {
        state: {
          map: {}
        },
        namespaced: true,
        mutations: {
          __updateStore__(state, value) {
            state.map = value
          }
        }
      })
    }
  
    setItem(key, value) {
      const map = Object.assign({}, this.vuex.state[this.moduleName].map)
      map[key] = value
      this.vuex.commit(this.moduleName + '/__updateStore__', map)
    }
  
    getItem(key) {
      const v = this.vuex.state[this.moduleName].map[key]
      if (typeof v === 'object') {
        return Object.assign({}, v)
      }
      return v
    }
  
    getKeys() {
      return Object.keys(this.vuex.state[this.moduleName].map)
    }
  
    removeItem(key) {
      const map = Object.assign({}, this.vuex.state[this.moduleName].map)
      delete map[key]
      this.vuex.commit(this.moduleName + '/__updateStore__', map)
    }
  
    clear() {
      this.vuex.commit(this.moduleName + '/__updateStore__', {})
    }
  }
  